﻿using HCDV.HCSP;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BsForApi
{
    public class GetCodeLogInfo
    {
        public static DataSet getCodeLog(string[] codeinfos)
        {
            DataSet ds = new DataSet();
            string taskid = "";
       
            using (dbsimple db = new dbsimple())
            {
                string csgetpc = "";
                string Ids = "";//ID
                #region 贴码记录信息
                DataTable dt_贴码记录信息 = db.Query(string.Format("SELECT * from hsc_task_process_workok_code   where (startcodes='{0}' or startcodes is null) and startcodel={2} and (startcoden<={1} and  endcoden>={1}) order by dataaddtime desc  LIMIT 0,1", codeinfos[0], codeinfos[1], codeinfos[2]));
                dt_贴码记录信息.TableName = "review_task_center_work_shutok";
                ds.Tables.Add(dt_贴码记录信息.Copy());
                if (dt_贴码记录信息.Rows.Count == 0) goto returnNowBack;
                taskid = dt_贴码记录信息.Rows[0]["taskid"].ToString();
                csgetpc = dt_贴码记录信息.Rows[0]["csgetpc"].ToString();
                if (csgetpc == "" || csgetpc == "null") csgetpc = "no";
                #endregion

                #region 基地库出库运输-发到加工中心的记录
                DataTable dt_基地库出库运输 = db.Query(string.Format("SELECT * from hsc_task_store_out where centerintime='{0}'  and  taskid={1}   ",
                 dt_贴码记录信息.Rows[0]["taskworktime"].ToString(), dt_贴码记录信息.Rows[0]["taskid"].ToString()));
                dt_基地库出库运输.TableName = "review_task_work_outstore";
                ds.Tables.Add(dt_基地库出库运输.Copy());
                //////////////////////////dt_基地库
                Ids = "";
                foreach (DataRow row in dt_基地库出库运输.Rows)
                {
                    Ids += row["datafromkeyid"] + ",";
                }
                if (Ids.Length >= 2)
                {
                    Ids = Ids.Substring(0, Ids.Length - 1);
                }
                else
                {
                    Ids = "0";
                    if (csgetpc == null || csgetpc == "" || csgetpc == "no")
                    {
                        goto returnNowBack;
                    }
                }
                #endregion

                #region 基地其它所有相关信息
                DataTable dt_基地库_最后的入库记录 = db.Query(string.Format("SELECT * from hsc_task_store where id in ({0}) or csgetpc='{1}' LIMIT 0,1", Ids, csgetpc));
                dt_基地库_最后的入库记录.TableName = "review_task_work_instore";//review_task_work_selfworkbackstore


                //////////////////////////dt_作业表_采摘
                var 采摘S = "";
                Ids = "";
                var 基地的入库方式 = "";
                foreach (DataRow row in dt_基地库_最后的入库记录.Rows)
                {
                    if (string.IsNullOrEmpty(基地的入库方式)) 基地的入库方式 = row["inouttype"].ToString();
                    Ids += row["datafromkeyid"] + ",";
                    采摘S += row["tasklistid"] + ",";
                }
                if (Ids.Length >= 2)
                {
                    Ids = Ids.Substring(0, Ids.Length - 1);

                }
                else
                {
                    Ids = "0";

                } if (采摘S.Length >= 2)
                {

                    采摘S = 采摘S.Substring(0, 采摘S.Length - 1);
                }
                else
                {

                    采摘S = "0";
                }

                if (基地的入库方式 == "加工完成入库")
                {
                    dt_基地库_最后的入库记录.TableName = "review_task_work_selfworkbackstore";//最后的应该是自加工入库
                    ds.Tables.Add(dt_基地库_最后的入库记录.Copy());


                    //自加工出库
                    DataTable dt_自加工出库 = db.Query(string.Format("SELECT * from hsc_task_process_work where  inouttype='自加工出库' and id in({0}) ",
                     Ids));
                    dt_自加工出库.TableName = "review_task_work_selfworkoutstore";
                    ds.Tables.Add(dt_自加工出库.Copy());

                    Ids = "";

                    foreach (DataRow row in dt_自加工出库.Rows)
                    {
                        Ids += row["datafromkeyid"] + ",";
                    }
                    if (Ids.Length >= 2)
                    {
                        Ids = Ids.Substring(0, Ids.Length - 1);
                    }
                    else
                    {
                        Ids = "0";
                    }


                    //采摘入库记录
                    DataTable dt_基地库_采摘入库记录 = db.Query(string.Format("SELECT * from hsc_task_store where  inouttype='采摘入库' and id in({0}) ",
                     Ids));
                    dt_基地库_采摘入库记录.TableName = "review_task_work_instore";
                    ds.Tables.Add(dt_基地库_采摘入库记录.Copy());

                }
                else
                {
                    dt_基地库_最后的入库记录.TableName = "review_task_work_instore";//最后的应该是采摘
                    ds.Tables.Add(dt_基地库_最后的入库记录.Copy());
                }


                DataTable dt_作业表_采摘 = db.Query(string.Format("SELECT * from hsc_task_info where id in ({0}) or csgetpc='{1}' and infobytype1=11 and infotype=11", 采摘S, csgetpc));
                dt_作业表_采摘.TableName = "review_task_work_get";
                ds.Tables.Add(dt_作业表_采摘.Copy());

                if (!string.IsNullOrEmpty(taskid))
                {
                    //////////////////////////dt_作业
                    DataTable dt_作业 = db.Query(string.Format("SELECT * from hsc_task_info where taskid={0} and infobytype1<>11 and infotype<>11",
                     taskid));
                    dt_作业.TableName = "review_task_work";
                    ds.Tables.Add(dt_作业.Copy());

                    //////////////////////////dt_任务
                    DataTable dt_任务 = db.Query(string.Format("SELECT * from hsc_task where id= {0} ", taskid));
                    dt_任务.TableName = "review_task";
                    ds.Tables.Add(dt_任务.Copy());
                }
                #endregion


                //5 中心库运输入库
                if (dt_基地库出库运输.Rows.Count == 0) goto returnNowBack;
                DataTable dt_中心库运输入库;
                dt_中心库运输入库 = db.Query(string.Format("SELECT * from hsc_task_store_process where  taskid ={0} and outgid='{1}'", taskid,
                      dt_基地库出库运输.Rows[0]["centeringuid"].ToString()));
                if (dt_中心库运输入库 == null || dt_中心库运输入库.Rows.Count == 0)
                {
                    dt_中心库运输入库 = db.Query(string.Format("SELECT * from hsc_task_store_process where   outgid='{0}'",
                       dt_基地库出库运输.Rows[0]["centeringuid"].ToString()));
                }
                if (dt_中心库运输入库 != null && dt_中心库运输入库.Rows.Count > 0 && dt_基地库出库运输 != null && dt_基地库出库运输.Rows.Count > 0)
                {

                    dt_中心库运输入库.Rows[0]["storeall"] = dt_基地库出库运输.Rows[0]["storeall"].ToString();//取发货的量
                }
                dt_中心库运输入库.TableName = "review_task_wayto_center";
                ds.Tables.Add(dt_中心库运输入库.Copy());
                if (dt_中心库运输入库.Rows.Count == 0) goto returnNowBack;


                //2 中心加工完成入库
                //DataTable 中心加工完成入库 = db.Query(string.Format("SELECT * from hsc_task_process_workok  where codegid in({0}) limit 0,1", codeGIds));
                //中心加工完成入库.TableName = "review_task_center_work_codeok";
                //ds.Tables.Add(中心加工完成入库.Copy());
                //    if (中心加工完成入库.Rows.Count == 0) goto returnNowBack;
                //    ///上游

                //    string thisIds = "";//本ID
                //    foreach (DataRow row in 中心加工完成入库.Rows)
                //    {
                //        if (string.IsNullOrEmpty(taskid))
                //        { taskid = row["taskid"].ToString(); }
                //        Ids += row["datafromkeyid"] + ",";
                //        thisIds += row["id"] + ",";
                //    }
                //    if (Ids.Length >= 2)
                //    {
                //        Ids = Ids.Substring(0, Ids.Length - 1);
                //        thisIds = thisIds.Substring(0, thisIds.Length - 1);
                //    }
                //    else
                //    {
                //        Ids = "0";
                //        thisIds = "0";
                //    }

                //    //3 dt_发到商超
                //    DataTable dt_发到商超 = db.Query(string.Format("SELECT * from hsc_task_process_workokout where datafromkeyid in({0})", thisIds));
                //    dt_发到商超.TableName = "review_task_center_work_ok_out";
                //    ds.Tables.Add(dt_发到商超.Copy());

                //    //4 中心出库加工
                //    if (中心加工完成入库.Rows[0]["datafromtable"].ToString() == "hsc_task_process_work")//从加工来的  
                //    {
                //        DataTable dt_取原料 = db.Query(string.Format("SELECT * from hsc_task_process_work where id in({0}) and inouttype='出库加工'", Ids));
                //        dt_取原料.TableName = "review_task_center_getwork";
                //        ds.Tables.Add(dt_取原料.Copy());
                //        //////////////////////////dt_中心库运输入库
                //        Ids = "";
                //        foreach (DataRow row in dt_取原料.Rows)
                //        {
                //            Ids += row["datafromkeyid"] + ",";
                //        }
                //        if (Ids.Length >= 2)
                //        {
                //            Ids = Ids.Substring(0, Ids.Length - 1);
                //        }
                //        else
                //        {
                //            Ids = "0";
                //        }
                //    }






            }
        returnNowBack:
            return ds;

          



        }
    }
}
